<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <link rel="stylesheet" type="text/css" href="css/avatar.css" media="all">
    <style type="text/css">

        body{background: #fafafa;}
        button{clear: both;display: block;padding:5px 15px;}
    </style>
</head>
<body>
<div class="avatarbox">
    <div class="imgproview_b">
        <div class="imgcenter">
            <input style="display: none" type="file" name="" id="uploadpic">
        </div>
    </div>
    <div class="imgproview">
        <div class="imgbox_b"><div></div><span>中尺寸头像50×50像素</span></div>
        <div class="imgbox_s"><div></div><span>小尺寸头像30×30像素</span></div>
    </div>
    <button>保存头像</button>
</div>
<script type="text/javascript" src="js/jQuery-1.7.1.min.js"></script>
<script type="text/javascript" src="uploadify/jquery.uploadify-3.1.min.js"></script>
<script type="text/javascript" src="js/jquery.Jcrop.js"></script>
<script type="text/javascript">
    $(function($){
        $("#uploadpic").uploadify({
            auto:true,
            height: 36,
            swf: 'uploadify/uploadify.swf',
            uploader: 'avatar.html',
            width: 101,
            buttonText:"",
            onUploadStart:function(file){
                $(".imgcenter").html("").addClass("uploading");
            },
            onUploadSuccess:function(file, data, response){
                userAvatar(data);//返回的data值为图片的地址
            },
            onUploadError:function(data){

            }
        });
        function userAvatar(picUrl){
            $(".imgcenter").html("<img src="+picUrl+" id='target' alt='Flowers' />").addClass("uploadcomplete");
            $(".imgbox_b div").append("<img src="+picUrl+" id='preview_b' alt='Preview' class='jcrop-preview' />");
            $(".imgbox_s div").append("<img src="+picUrl+" id='preview_s' alt='Preview' class='jcrop-preview' />");
            $(".imgproview").append('<div class="imgbox_s"><a href="javacript:;">重新上传</a></div>');
            var jcrop_api, boundx, boundy;
            $('#target').Jcrop({onChange: updatePreview,onSelect: updatePreview,bgOpacity:   .8,aspectRatio: 1},function(){
                var bounds = this.getBounds();boundx = bounds[0];boundy = bounds[1];jcrop_api = this;});
            function updatePreview(c){
                if (parseInt(c.w) > 0){
                    var rxb = 50 / c.w,ryb = 50 / c.h,rxs = 30 / c.w,rys = 30 / c.h;
                    $('#preview_b').css({width: Math.round(rxb * boundx) + 'px',height: Math.round(ryb * boundy) + 'px',marginLeft: '-' + Math.round(rxb * c.x) + 'px', marginTop: '-' + Math.round(ryb * c.y) + 'px'});
                    $('#preview_s').css({width: Math.round(rxs * boundx) + 'px',height: Math.round(rys * boundy) + 'px',marginLeft: '-' + Math.round(rxs * c.x) + 'px',marginTop: '-' + Math.round(rys * c.y) + 'px'});
                }
            };
        }
    });
</script>
</body>
</html>